﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tool;
using TP5Like.DBModel;
using TP5Like.DBModelValidate;

namespace TP5Like.BLL
{
    public class Address: AddressModelRule
    {
        private DataDBContext _context;
        //S_BLL sb = null;

        public Address()
        {
            
        }

        public Address(DataDBContext context)
        {
            _context = context;
            //sb = new S_BLL(_context);
        }

        public object Get()
        {
            S_Result.data = _context.Addresses.ToList();
            if (S_Result.data == null)
            {
                S_Result.error = "暂无此数据";
                return false;

            }
            

            return S_Result.data;
        }

        public object GetId(int id)
        {
            S_Result.data = _context.Addresses.Where(x => x.Id == id).FirstOrDefault();
            if (S_Result.data == null)
            {
                S_Result.error = "暂无此数据";
                return false;

            }
            return S_Result.data;
        }

        public List<TP5Like.DBModel.Address> GetIdZ(int id)
        {
            var addressList = _context.Addresses.Where(x => x.Id == id).ToList();
            if (addressList == null)
            {
                return addressList;

            }
            return addressList;
        }

        public object post(DBModel.Address value)
        {
            if(!getIsOK<DBModel.Address>(value))
            {
                return false;
            }

            _context.Addresses.Add(value);
            int a= _context.SaveChanges();

            if(a>0)
            {
                S_Result.msg = "成功";
                return true;
            }
            else
            {
                S_Result.msg = "添加失败";
                return false;
            }
        }
    }
}
